import actionType from "../actions/actionType"
const initState = [
    {
        id: 1,
        title: 'Apple',
        price: 8199,
        amount: 10
    },
    {
        id: 2,
        title: 'orange',
        price: 6299,
        amount: 12
    }
]

// 创建 购物车的 reducer ， 参数是 state 和 action
export default (state=initState, action) => {
    switch(action.type) {
        case actionType.CART_AMOUNT_DECREMENT:
            return state.map(item => {
                if(item.id === action.payload.id) {
                    item.amount -= 1
                }
                return item
            })
        case actionType.CART_AMOUNT_INCREMENT:
            return state.map(item => {
                if(item.id === action.payload.id) {
                    item.amount += 1
                }
                return item
            })
        default: 
            return state
    }
}